package com.br.mercarad.db.vos;

public class CampoVO {
	
	private String nome;
	private String columnName;
	private int type;
	private Boolean  isNull;
	private Boolean isPrimary;
	private Boolean isFk;
	private String typeOracle;
	
	public class TypesLite{
		public static final int TEXT = 0;
		public static final int INTEGER = 1;
		public static final int REAL = 2;
		public static final int FLOAT = 3;
	}
	public class TypesOracle{
		public static final int VARCHAR2 = 0;
		public static final int NUMBER = 1;
		public static final int DATE = 2;
	}
	
	public String getTypeDesc(){
		switch (type) {
		case 0:
			return "Text";
		case 1:
			return "Integer";
		case 2:
			return "Real";
		case 3:
			return "Float";
		default:
			return "ERRONOTIPO";
		}
	}
	
	
	public String getTypeJavaPrimitive(){
		switch (type) {
		case 1:
			return "int";
		case 2:
			return "float";
		case 0:
			if (nome.toLowerCase().contains("dt")||nome.toLowerCase().contains("data"))
				return "LocalDate";
			else
				return "String";
		
		default:
			return "ERRONOTIPOPRIMITIVE";
		}
	}
	
	public String getTypeJavaObject(){
		switch (type) {
		case 1:
			return "Integer";
		case 2:
			return "Float";
		case 0:
			if (nome.toLowerCase().contains("dt")||nome.toLowerCase().contains("data"))
				return "Date";
			else
				return "String";
		default:
			return "ERRONOTIPOPRIMITIVE";
		}
	}
	
	public CampoVO() {
		isNull = true;
		isPrimary = false;
		isFk = false;
	}
	
	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	public int getType() {
		return type;
	}
	
//	public void setType(int type) {
//		this.type = type;
//	}
	public Boolean getIsNull() {
		return isNull;
	}
	public void setIsNull(Boolean isNull) {
		this.isNull = isNull;
	}
	public Boolean getIsPrimary() {
		return isPrimary;
	}
	public void setIsPrimary(Boolean isPrimary) {
		this.isPrimary = isPrimary;
	}
	public Boolean getIsFk() {
		return isFk;
	}
	public void setIsFk(Boolean isFk) {
		this.isFk = isFk;
	}

	public void setTypeOracle(String typeOracle,int precision,int scale) {

		if (typeOracle.equals("NUMBER") && scale>0)
			this.type = 2;
		else if (typeOracle.equals("NUMBER"))
			this.type = 1;
		else  {
			this.type = 0;
		}
		this.typeOracle = typeOracle;
	}
	
	public void setTypeSqlite(String typeSqlite){
		if (typeSqlite.equalsIgnoreCase("TEXT")){
			this.type = 0;
		}else if (typeSqlite.equalsIgnoreCase("INTEGER")) {
			this.type = 1;
		}else if (typeSqlite.equalsIgnoreCase("REAL")) {
			this.type = 2;
		}else if (typeSqlite.equalsIgnoreCase("FLOAT")) {
			this.type = 3;
		}
		
		
			
			
//			else if (sqls[i].equals("$TEXT$"))
//				campo.setType(0);
//			else if (sqls[i].equals("$INTEGER$"))
//				campo.setType(1);
//			else if (sqls[i].equals("$REAL$"))
//				campo.setType(2);
//			else if (sqls[i].equals("$FLOAT$"))
//				campo.setType(3);
	}
	
	public String getNomeSemAlias(){
		
		return this.nome.split("_")[0]; 
	}

	public String getStringSqlite() {
		StringBuilder sb = new StringBuilder();
		sb.append("\""+this.nome+"\"");
		sb.append(" ");
		sb.append(getTypeDesc());
		if (!isNull)
			sb.append(" NOT NULL");
		return sb.toString();
	}
	
	public String getStringDA(String tableName){
		StringBuilder sb = new StringBuilder();
		sb.append("values.put(Tables."+tableName+"."+this.columnName);
		sb.append(", entity.get"+this.columnName+"()");
		return sb.toString();
		//values.put(Tables.Pedidos.codigo, entity.getCodigo());
	}

	public String getPatterSqLiteNaoCompleto() {
		StringBuilder sb = new StringBuilder();
		sb.append("\""+this.nome+"\"");
//		sb.append(" ");
//		sb.append(getTypeDesc());
//		if (!isNull)
//			sb.append(" NOT NULL");
		return sb.toString();
	}
	public String getPatternTables(){
		StringBuilder sb = new StringBuilder();
		sb.append("public final static String ");
		sb.append(getNomeSemAlias()+" = ");
		sb.append("\""+this.nome+"\"");
		
		return sb.toString();
	}
	
	public String getStringVo() {
		StringBuilder sb = new StringBuilder();
		sb.append("private ");
		sb.append(getNomeSemAlias()+" ");
		sb.append(getTypeJavaPrimitive());
		sb.append(";");
		return sb.toString();
	}
	public String getStringTables(){
		StringBuilder sb = new StringBuilder();
		
		sb.append("public final static String "+this.getNomeSemAlias()+" = \""+this.nome+"\";");	
		
		return sb.toString();
	}
}
